package com.ds.common.route.config;

import com.ds.common.nacos.ConfigModel;
import lombok.Getter;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 集群配置
 */
public class ClusterConfigList implements ConfigModel {

    Map<String, ClusterConfig> clusterConfigMap;
    @Getter
    List<ClusterConfig> clusterConfigList;

    public void setClusterConfigList(List<ClusterConfig> clusterConfigList) {
        this.clusterConfigList = clusterConfigList;
        if (clusterConfigList != null && clusterConfigList.stream().allMatch(ClusterConfig::isValid)) {
            clusterConfigMap = clusterConfigList.stream().collect(Collectors.toMap(ClusterConfig::getName, Function.identity()));
        }
    }

    public ClusterConfig getClusterConfig(String clusterName) {
        return clusterConfigMap.get(clusterName);
    }
    @Override
    public boolean isValid() {
        return clusterConfigList != null && clusterConfigList.stream().allMatch(ClusterConfig::isValid);
    }

}
